/** 
 * 项目名称:Sprider-WebMagic-bak 
 * 文件名称:MaoYanSearchPageProcessor.java 
 * 包名:com.xiaotu.spider.maoyan.pageprocessor 
 * 创建日期:2020年1月2日上午9:28:16 
 * Copyright (c) 2020, zxaiye1314@163.com All Rights Reserved.</pre> 
 */  
package com.xiaotu.spider.maoyan.pageprocessor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.MaoYanSearchMovieModel;
import com.xiaotu.common.redis.RedisClientTemplate;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.SpringContextHolder;
import com.xiaotu.common.util.UUIDUtils;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.Html;

/**
 * 
 * 项目名称：Sprider-WebMagic    
 * 类名称：MaoYanSearchMovieSupplementPageProcessor    
 * 类描述：    
 * 创建人：张晓   
 * 创建时间：2020年1月8日 下午5:46:36    
 * @version
 */
@Component("MaoYanSearchMovieSupplementPageProcessor")
public class MaoYanSearchMovieSupplementPageProcessor extends APageProcessor {
	
	private static final String BASEINFO_URL = "http://piaofang.maoyan.com/movie/%s?_v_=yes";//基础信息
	
	private String [] tvArr = new String[]{"环城七十里",
		"断片儿",
		"可爱的中国大陆",
		"天生有福",
		"暴力街区",
		"日照重庆",
		"海洋",
		"过境",
		"浮士德",
		"大力神",
		"阿郎的故事",
		"熊出没·狂野中国大陆",
		"使命召唤",
		"拐杖",
		"我们诞生在中国大陆",
		"大太阳",
		"蓝天使",
		"华尔街：金钱永不眠",
		"棋王",
		"神通佛影",
		"看见我和你",
		"王的盛宴",
		"美丽战争",
		"特警判官",
		"泰迪熊之玩具大战",
		"致我们终将逝去的青春",
		"破天荒",
		"张艺谋和他的“影”",
		"年轻的阿迈德",
		"微微一笑很倾城",
		"东北往事之破马张飞",
		"异兽来袭",
		"妈阁是座城",
		"夏日示爱",
		"疯狂的外星人",
		"特别追踪",
		"午夜凶梦",
		"从哪来，到哪去",
		"极地大冒险2",
		"烈日灼心",
		"一条叫王子的狗",
		"枪过境",
		"极限特工：终极回归",
		"师父",
		"黄金兄弟",
		"谁动了我的梦想",
		"致命请柬",
		"超能太阳鸭",
		"老五的奥斯卡",
		"你的名字。",
		"过春天",
		"巧虎大飞船历险记",
		"哺乳期的女人",
		"宇宙英雄之超银河传说",
		"情剑",
		"非常营救",
		"阿婆的槟榔",
		"对不起，我们错过了你",
		"我要去远方",
		"等你追我",
		"南方",
		"第十二夜",
		"特工强档",
		"青春时光",
		"消失",
		"你往哪里跑",
		"青春.com",
		"不要回头",
		"夺命推理",
		"虹猫蓝兔火凤凰",
		"0.844444444",
		"逆生·致亲爱的小孩",
		"红星照耀中国大陆",
		"黎明之前",
		"桔子的天空",
		"街舞小子",
		"老虎都要嫁",
		"变化中的中国大陆·生活因你而火热",
		"老无所依",
		"武林外传",
		"黑色闪电",
		"再吻我一次",
		"无底洞",
		"泡菜",
		"刽子手",
		"黑月",
		"动物特工局",
		"爱情36计",
		"费加罗的婚礼",
		"地牢围攻",
		"宝贝，对不起",
		"敦刻尔克大撤退",
		"四百击",
		"家园",
		"金秋鹿鸣",
		"城南庄1948",
		"肥伊不容易",
		"茶花女",
		"恶灵之门",
		"嫌疑人X的献身",
		"劳拉的星星在中国大陆",
		"波西·杰克逊与魔兽之海",
		"精舞门2",
		"必是天堂",
		"一触即发",
		"孩子王",
		"孤岛",
		"忍者神龟2",
		"闽宁镇",
		"背水一战",
		"正·青春",
		"赶牲灵",
		"虎胆追凶",
		"砂制时镜下的疗养院",
		"魔侠传之唐吉可德",
		"大熊猫传奇"};
//	private String [] tvArr = new String[]{
//		"黄金兄弟",
//		"谁动了我的梦想",
//		};
	
	private Map<String,String> cookies = new HashMap<String,String>();
	
//	private List<JSONObject> jsonList = null;
//	
//	{ 
//		cookies.put("Cookie", PropertiesUtil.getProperty("BoxOffice.site.Cookie"));
//		
//		RedisClientTemplate redisClient = (RedisClientTemplate)SpringContextHolder.getBean("redisClientTemplate",RedisClientTemplate.class);
//		
//		Map<String, String> targetMap = redisClient.hgetAll("MAOYAN_SEARCH_MOVIE");
//		
//		List<String> targetList = new ArrayList<String>(targetMap.values());
//		jsonList = new ArrayList<JSONObject>();
//        for (String string : targetList)
//        {
//            if (StringUtils.isEmpty(string))
//                continue;
//            try
//            {
//                JSONObject jsonObj = GsonUtils.fromJson(string,
//                        JSONObject.class);
//                jsonList.add(0, jsonObj);
//            }
//            catch (Exception e)
//            {
//            	
//            }
//        }
//	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected Request getStartRequest(JSONObject target) {
		
		if(null == target.get("totalBoxoffice") && null != target.get("area") && null != target.get("label")){
			boolean flag = false;
//			for(JSONObject json :jsonList){
//				if(target.getString("tvname").equals(json.getString("tvname"))){
//					flag = true;
//				}
//			}
			for(String json :tvArr){
				if(target.getString("tvname").equals(json)){
					flag = true;
				}
			}
			if(flag){
				Request request = new Request(String.format(BASEINFO_URL, target.getString("dataid")));
				request.setExtras((Map<String, Object>)target);
				return request;
			}else{
				return null;
			}
		}
		return null;
		
	}
	
	@Override
	public void process(Page page) {
		
		Map<String, Object> extrasMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.BASICINFO:
                this.basicinfoProcess(page,extrasMap);
                break;
        }
		
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_BOXOFFICE;
	}

	@Override
	public String getPageDataType(Page page) {
		
		String url = page.getUrl().toString();
        if (url.contains("_v_=yes")){
        	return Constants.PageDataType.BASICINFO;
        }
        return null;
	}

	/**
	 * basicinfoProcess(基本信息)   
	 * 创建人：张晓   
	 * 创建时间：2020年1月9日 上午8:41:24    
	 * 修改备注： 
	 * @param page
	 * @param extraMap
	 */
	private void basicinfoProcess(Page page,Map<String, Object> extrasMap) {
		
		String url = page.getRequest().getUrl();
		
		String tvname = extrasMap.get("tvname").toString();
		
		String dataId = RegexUtils.regexesSearch(url, RegexUtils.REGEX_INTEGER);
		
		//解密数据
        Html html = PageUtils.spiderAudienceRating(page,cookies);
        
        //票房相关
        String totalBoxoffice = null;// 累计票房
        
        List<String> checkDataList = new ArrayList<String>();
        //累计票房相关
        checkDataList = html.xpath
        		("//div[@class='movie-container']//div[@class='info-detail-row']").all();
        if (checkDataList.size()>0){
        	String boxOfficeLine =html.xpath
            		("//div[@class='movie-container']//div[@class='info-detail-row']").all().get(0);
            Html htmlUp = new Html(boxOfficeLine);
            List<String> allUp = htmlUp.xpath("//div[@class='info-detail-row']//div").all();
            if(new Html(allUp.get(0)).xpath("//p[@class='info-detail-title']").get().trim().contains("累计票房")){
            	totalBoxoffice = getFormattedData(new Html(allUp.get(0)).xpath("//p[@class='info-detail-content']").get()).replace("--", "").replace("暂无", "");
            }
        }else{
        	return;
        }
		MaoYanSearchMovieModel maoYanSearchMovieModel = new MaoYanSearchMovieModel(
				UUIDUtils.getId(), null, dataId, tvname, null, null, null, null,
				null, null, totalBoxoffice, null,
				null, null, null, null,
				null, null, null, null, null,
				null, null, null, null, null,
				null, null, null, null
				);
		if(null!=totalBoxoffice){
			page.putField(MaoYanSearchMovieModel.class.getName(), maoYanSearchMovieModel);
		}
	}
	
	private String getFormattedData(String data){
        return RegexUtils.replaceStr(HtmlUtil.delHTMLTag(data), SepratorUtil.SEP_SPACE_EN,
                StringUtils.EMPTY);
    }
}
